package javaapplication1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;//添加包

public class tank extends JFrame{
	MyPanel mp=null;
	public static void main(String[] args){
		tank t1=new tank();
	}

	public tank(){
		mp=new MyPanel();
		this.add(mp);
		this.addKeyListener(mp);//添加监听

		this.setSize(400,300);
		this.setLocation(300,280);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}
class MyPanel extends JPanel implements KeyListener{//添加键盘监听接口
	MyTank mt=null;

	public MyPanel(){
		mt=new MyTank(140,232);
		Vector<Enemy> enemy=new Vector<Enemy>();//把坦克做成集合类（多线程）,用泛型Enemy
			//用户控制用单线程(List)，随机时用多线程(Vector)
		int Enum=3;

		for(int i=0;i<Enum;i++){
			Enemy ene=new Enemy((i)*100+5,0);
			enemy.add(ene);//添加1个敌人
		}
	}

	public void paint(Graphics g){
		super.paint(g);
		g.fillRect(0,0,400,300);
		this.drawTank(mt.getX(),mt.getY(),g,0,0);
	}
	public void drawTank(int x,int y,Graphics g,int fangxiang,int leixing){
		switch(leixing){
			case 0:
				g.setColor(Color.yellow);break;
			case 1:
				g.setColor(Color.green);break;
		}
		switch(fangxiang){
			case 0:
				g.fill3DRect(x,y,5,30,false);
				g.fill3DRect(x+15,y,5,30,false);
				g.fill3DRect(x+5,y+5,10,20,false);
				g.fillOval(x+4,y+10,10,10);
				g.drawLine(x+10,y+15,x+10,y-3);
				break;
			case 1:
				g.fill3DRect(x,y,30,5,false);
				g.fill3DRect(x,y+15,30,5,false);
				g.fill3DRect(x+5,y+5,20,10,false);
				g.fillOval(x+10,y+4,10,10);
				g.drawLine(x+15,y+10,x-3,y+10);
				break;
			case 2:
				g.fill3DRect(x,y,5,30,false);
				g.fill3DRect(x+15,y,5,30,false);
				g.fill3DRect(x+5,y+5,10,20,false);
				g.fillOval(x+4,y+10,10,10);
				g.drawLine(x+10,y+15,x+10,y+33);
				break;
			case 3:
				g.fill3DRect(x,y,30,5,false);
				g.fill3DRect(x,y+15,30,5,false);
				g.fill3DRect(x+5,y+5,20,10,false);
				g.fillOval(x+10,y+4,10,10);
				g.drawLine(x+15,y+10,x+33,y+10);
				break;
		}
	}

	public void keyTyped(KeyEvent e){}//需要重写的三个函数
	public void keyReleased(KeyEvent e){}
	public void keyPressed(KeyEvent e){
		if(e.getKeyCode()==KeyEvent.VK_W){
			this.mt.setFangxiang(0);
			this.mt.xiangshang();
		}else if(e.getKeyCode()==KeyEvent.VK_A){
			this.mt.setFangxiang(1);
			this.mt.xiangzuo();
		}else if(e.getKeyCode()==KeyEvent.VK_S){
			this.mt.setFangxiang(2);
			this.mt.xiangxia();
		}else if(e.getKeyCode()==KeyEvent.VK_D){
			this.mt.setFangxiang(3);
			this.mt.xiangyou();
		}
		this.repaint();
	}
class Tank{
	int x=0,y=0;
	int fangxiang=0;
	int sudu=10;//速度1像素
	
	public int getSudu(){
		return sudu;		
	}
	
	public void setSudu(){
		this.sudu=sudu;
	}

	public int getFangxiang(){
		return fangxiang;
	}

	public void setFangxiang(int fangxiang){
		this.fangxiang=fangxiang;
		
	}

	public int getX(){
		return x;
	}

	public int getY(){
		return y;
	}
	
	public void setY(int y){
		this.y=y;
	}
	
	public Tank(int x,int y){
		this.x=x;
		this.y=y;
	}
}
class MyTank extends Tank{
	public MyTank(int x,int y){
		super(x,y);
	}
	public void xiangshang(){//移动
		y-=sudu;
	}
	public void xiangzuo(){
		x-=sudu;
	}
	public void xiangxia(){
		y+=sudu;
	}
	public void xiangyou(){
		x+=sudu;
	}
}
class Enemy extends Tank{
	public Enemy(int x,int y){
		super(x,y);//空方法
	}
}
}